// pages/search/index.js
import { regeneratorRuntime } from '../../lib/runtime/runtime'
import { request } from '../../request/index'

// /search?keywords=海阔天空

Page({

  /**
   * 页面的初始数据
   */
  data: {
    listCur: [],
    name: ''
  },
  //获取搜索条件
  getCur(e) {
    // console.log(e.detail.value)
    this.setData({
      name: e.detail.value
    })
  },
  // 根据条件进行查询
  async findByName() {
    var name = this.data.name;
    if(name.length>0){
      let result = await request({
        url: '/search?keywords=' + name
      })
      var index = result.result.songs;
      console.log(index)
      this.setData({
        listCur: index
      })
    }
  },
  // 获取音乐播放信息
  async getMug(e){
    // 解析参数
    var dataset = e.currentTarget.dataset;
    let result = await request({
      url: '/song/url?id=' + dataset.id
    })
    var index = result.data;
    console.log(index[0].url)
    
    // 后台音乐播放
    const backgroundAudioManager = wx.getBackgroundAudioManager()
    backgroundAudioManager.title = dataset.name;
    // backgroundAudioManager.epname = '此时此刻';
    // backgroundAudioManager.singer = '许巍';
    // backgroundAudioManager.coverImgUrl = 'http://y.gtimg.cn/music/photo_new/T002R300x300M000003rsKF44GyaSk.jpg?max_age=2592000';

    // 设置了 src 之后会自动播放
    backgroundAudioManager.src = index[0].url;
    backgroundAudioManager.play();
    backgroundAudioManager.onPlay(() => {
          console.log("音乐播放开始");
      })
    backgroundAudioManager.onEnded(() => {
          console.log("音乐播放结束");
      })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    this.findByName()
  },
})